package br.ufal.questione.web;

import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import br.ufal.questione.dao.AvaliacaoDao;
import br.ufal.questione.dao.QuestionarioDao;
import br.ufal.questione.domain.Avaliador;
import br.ufal.questione.domain.Questionario;
import br.ufal.questione.domain.TipoQuestionario;

@Controller
@RequestMapping(value = "questionarios")
@SessionAttributes("avaliador")
public class QuestionarioController implements Serializable {

	private static final long serialVersionUID = -4601227148069840412L;
	
	private Logger logger = LoggerFactory.getLogger(QuestionarioController.class);

	@Autowired
	private QuestionarioDao questionarioDao;
	
	@Autowired
	private AvaliacaoDao avaliacaoDao;
	
	@RequestMapping(method = RequestMethod.GET)
	public String index(Model model, Avaliador avaliador) {
		List<Questionario> questionarios = this.questionarioDao.listar();
		
		model.addAttribute("questionarios", questionarios);
		
		return "questionarios";
	}

	
	@RequestMapping(value = "{idQuestionario}", method = RequestMethod.GET)
	public String carregarQuestionario(Model model, HttpSession session, @PathVariable("idQuestionario") Long idQuestionario, Avaliador avaliador) {		
		
		
		Questionario questionario = this.questionarioDao.consultar(idQuestionario);
		session.setAttribute("questionario", questionario);
		if (this.avaliacaoDao.hasAnswered(avaliador, questionario)) {
			model.addAttribute("mensagem", "questionario.respondido");
			return index(model, avaliador);
		}
		
		model.addAttribute("questionario", questionario);
		
		return "perguntas";
	}
	
	@RequestMapping(value = "{idQuestionario}/excluir", method = RequestMethod.GET)
	public String excluir(Model model, @PathVariable("idQuestionario") Long idQuestionario) {		
		Questionario questionario = this.questionarioDao.consultar(idQuestionario);
		
		this.questionarioDao.excluir(questionario);
		
		return "redirect:/questionarios";
	}
	
	@RequestMapping(value = "novo", method = RequestMethod.GET)
	public String setUpForm(Model model) {
		model.addAttribute("tiposQuestionario", TipoQuestionario.values());
		
		return "novo";
	}
	
	@RequestMapping(value = "novo", method = RequestMethod.POST)
	public String salvar(Model model, Questionario questionario) {		
		this.questionarioDao.inserir(questionario);
		
		model.addAttribute("mensagem", "questionario.salvar.sucesso");
		
		return "redirect:/questionarios";		
	}
	
	@RequestMapping(value = "{idQuestionario}/editar", method = RequestMethod.GET)
	public String setUpEditar(Model model, @PathVariable("idQuestionario") Long idQuestionario) {		
		Questionario questionario = this.questionarioDao.consultar(idQuestionario);
		
		model.addAttribute("questionario", questionario);
		model.addAttribute("tiposQuestionario", TipoQuestionario.values());
		
		return "questionario";		
	}
	
}